import Taro from "@tarojs/taro"
import {pageToLogin} from "./utils"
import {HTTP_STATUS} from './config'

const customInterceptor = (chain) => {

    const requestParams = chain.requestParams

    return chain.proceed(requestParams).then(res => {
        console.log(res.data)
        // 只要请求成功，不管返回什么状态码，都走这个回调
        if (res.data.code === HTTP_STATUS.CREATED) {
            return Promise.reject("请求资源不存在")

        } else if (res.statusCode === HTTP_STATUS.BAD_GATEWAY) {
            return Promise.reject("服务端出现了问题")
        } else if (res.statusCode === HTTP_STATUS.AUTHENTICATE) {
            Taro.setStorageSync("Authorization", "")
            pageToLogin()
            return Promise.reject("需要鉴权")
        } else if (res.data.code === HTTP_STATUS.SUCCESS) {
            return res.data
        }
    })
}

const interceptors = [customInterceptor]

export default interceptors
